//
//  SettingViewController.m
//  iLiteNote
//
//  Created by xiangbc on 16/3/18.
//  Copyright © 2016年 LiteNice. All rights reserved.
//

#import "SettingViewController.h"
#import "AppUtility.h"
#import "GitInfo.h"
#import "GitStateVC.h"
#import "SettingTableViewCell.h"

static NSString *GitSettingID = @"GitSettingCell";

@interface SettingViewController () {
    NSArray *_settingArray;
}

@end


@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.0];
    
    _settingArray = @[@"gitURL", @"账号", @"密码", @"目录", @"大小", @"日志"];
    
    CGRect frame = self.view.frame;
    CGRect r = CGRectMake(0, 0, frame.size.width, frame.size.height);
    UITableView *table = [[UITableView alloc] initWithFrame:r style:UITableViewStyleGrouped];
    table.layer.borderWidth = 1.0;
    table.layer.borderColor = [[UIColor colorWithRed:0.86 green:0.86 blue:0.86 alpha:1.0] CGColor];
    //table.bounces = NO;
    table.delegate = self;
    table.dataSource = self;
    
    
    [self.view addSubview:table];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return _settingArray.count;
    } else {
        return 1;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

/*- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"git info";
    }
    return @"";
}*/

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (indexPath.section == 0) {
        SettingTableViewCell *cell = [[SettingTableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:GitSettingID];
        
        NSInteger index = indexPath.row;
        cell.textLabel.text = _settingArray[index];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.textColor = [UIColor grayColor];
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
        cell.textLabel.frame = CGRectMake(0, 0, 40, 40);
        
        cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
        cell.detailTextLabel.textColor = [UIColor blackColor];
        
        GitInfo *info = [GitInfo shareGitInfo];
        
        if (index == 0) {
            cell.detailTextLabel.text = info.gitURL;
        } else if (index == 1) {
            cell.detailTextLabel.text = info.account;
        } else if (index == 2) {
            cell.detailTextLabel.text = @"******";
        } else if (index == 3) {
            cell.detailTextLabel.text = info.locatePath;
        } else if (index == 4) {
            float s = [AppUtility folderSizeAtPath:[info getGitPath]];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%0.2fMB", s];
        }
        return cell;
        
    }  else if (indexPath.section == 1) {
        UITableViewCell *cell = [[UITableViewCell alloc]  initWithStyle:UITableViewCellStyleDefault reuseIdentifier:GitSettingID];
        cell.textLabel.text = @"删除当前git笔记";
        cell.backgroundColor = [UIColor redColor];
        return cell;
    }
    return nil;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row <= 4) {
            return NO;
        }
        if (indexPath.row == 5) {
            GitStateVC * vc = [[GitStateVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    } else if (indexPath.section == 1) {
        [self deleteGit];
    }
    return NO;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (void)deleteGit {
    GitInfo *info = [GitInfo shareGitInfo];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"删除所有git" message:info.locatePath preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
    }];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        NSLog(@"deleteFold");
        [AppUtility deleteFolder:[info getGitPath]];
        info.gitURL = nil;
        info.account = nil;
        info.password = nil;
        info.locatePath = nil;
        [info WriteGitInfo];
        
        if (self.treeViewReloadBlock) {
            self.treeViewReloadBlock();
        }
    }];
    [alert addAction:cancel];
    [alert addAction:ok];
    [self presentViewController:alert animated:YES completion:nil];
}

@end






